<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Carbon\Carbon;

class StoreSetting extends Model
{
    protected $table = 'ims_yz_store_setting';
    protected $guarded = [''];
    protected $casts = [
        'value' => 'json'
    ];
    protected $hidden = ['store_id'];

    public $timestamps = true;

    public function getDateFormat()
    {
        return 'U';
    }

    public function getCreatedAtAttribute($value)
    {
        return $value ? Carbon::createFromTimestamp($value) : null;
    }

    public function getUpdatedAtAttribute($value)
    {
        return $value ? Carbon::createFromTimestamp($value) : null;
    }

    /**
     * 根据门店ID获取设置
     */
    public static function getStoreSettingByStoreId($store_id)
    {
        return self::select()->byStoreId($store_id);
    }
    
    /**
     * 根据门店ID和键名获取设置
     */
    public static function getStoreSettingByStoreIdAndByKey($store_id, $key)
    {
        return self::select()->byStoreId($store_id)->byKey($key);
    }
    
    /**
     * 按门店ID筛选
     */
    public function scopeByStoreId($query, $store_id)
    {
        return $query->where('store_id', $store_id);
    }
    
    /**
     * 按键名筛选
     */
    public function scopeByKey($query, $key)
    {
        return $query->where('key', $key);
    }
    
    /**
     * 设置门店设置
     */
    public static function setStoreSetting($store_id)
    {
        $setting_data = self::getStoreDefaultSetting();
        foreach ($setting_data as $key => $value) {
            self::create([
                'store_id' => $store_id,
                'key' => $key,
                'value' => $value
            ]);
        }
    }
    
    /**
     * 获取门店默认设置
     */
    private static function getStoreDefaultSetting()
    {
        return self::getDefaultSetting();
    }
    
    /**
     * 获取默认设置
     */
    private static function getDefaultSetting()
    {
        return [
            'store' => [
                'shop_commission' => 0,
                'settlement_day' => 0
            ],
            'point' => [
                'set' => [
                    "money_max" => 0,
                    "give_point" => 0,
                    "shop_award_point" => 0
                ]
            ],
            'love' => [
                'deduction' => 1,
                'deduction_proportion' => 0,
                'award' => 1,
                'award_proportion' => 0,
                'award_shop' => 0
            ],
            'discount' => [
                'discount_method' => 1,
                'discount_value' => []
            ],
            'commission' => [
                'is_commission' => 1,
                'hascommission' => 1,
                'level' => 3,
                'first_level' => 0,
                'second_level' => 0,
                'third_level' => 0,
                'rule' => []
            ],
            'team-dividend' => [
                'is_dividend' => 1,
                'has_dividend' => 1,
                'has_dividend_rate' => 0
            ],
            'area-dividend' => [
                'is_dividend' => 1,
                'has_dividend' => 1,
                'has_dividend_rate' => 0
            ],
            'merchant' => [
                'is_open_bonus_staff' => 1,
                'is_open_bonus_center' => 1,
                'staff_bonus' => 0
            ],
            'single-return' => [
                'is_single_return' => 1,
                'return_rate' => 0
            ],
            'full-return' => [
                'is_open' => 0
            ]
        ];
    }
    
    /**
     * 查询门店对应的权限配置信息
     */
    public static function getPluginViaSet($store_id, $key, $plugin_name)
    {
        $store_set = self::where('store_id', $store_id)->where('key', $key)->first();
        return $store_set['value'][$plugin_name] ?? null;
    }
    
    /**
     * 设置门店插件权限设置
     */
    public static function setStorePluginViaSetting($store_id)
    {
        $data = [];
        
        if ($data) {
            self::create([
                'store_id' => $store_id,
                'key' => 'plugin_via_basic',
                'value' => $data
            ]);
        }
    }
    
    /**
     * 与门店的关系
     */
    public function store()
    {
        return $this->belongsTo(Store::class, 'store_id');
    }
}
